﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DoAn
{
    public partial class frGiaoDienChinh : Form
    {
        Button btnRefresh, btnThem, btnTim, btnSua, btnXoa, btnXem;        
        public frGiaoDienChinh()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            GiaoDienChung();
            HangHoa_GUI();
            XuatHangHoa();
        }
        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        private void GiaoDienChung()
        {
            btnRefresh = new Button();
            btnThem = new Button();
            btnTim = new Button();
            btnSua = new Button();
            btnXem = new Button();
            btnXoa = new Button();
            btnRefresh.Width = btnThem.Width = btnTim.Width = btnSua.Width = btnXoa.Width = btnXem.Width = 90;
            btnRefresh.Height = btnThem.Height = btnTim.Height = btnSua.Height = btnXoa.Height= btnXem.Height = 23;
            btnRefresh.Top = btnThem.Top = btnTim.Top = btnSua.Top = btnXoa.Top = btnXem.Top = 80;
            btnRefresh.Left = 20;
            btnThem.Left = 120;
            btnTim.Left = 220;
            btnXem.Left = 320;
            btnSua.Left = 420;
            btnXoa.Left = 520;            
        }
        private void HangHoa_GUI()
        {
            lbTieuDe.Text = "Quản lý hàng hóa";

            btnRefresh.Name = "btnRefresh";
            btnRefresh.Text = "Refresh";
            btnRefresh.Click += (s, args) =>
            {
                XuatHangHoa();
            };

            btnThem.Name = "btnThemHangHoa";
            btnThem.Text = "Thêm hàng hóa";
            btnThem.Click += (s, args) =>
            {
                frThemHang frth = new frThemHang();
                if (frth.ShowDialog() == DialogResult.Cancel)
                    XuatHangHoa();
            };

            btnTim.Name = "btnTimHangHoa";
            btnTim.Text = "Tìm hàng hóa";
            btnTim.Click += (s, args) =>
            {
                MessageBox.Show("Tìm thấy rồi.");
            };

            btnSua.Name = "btnSua";
            btnSua.Text = "Cập nhật";
            btnSua.Click += (s, args) =>
            {
                frmSuaHang frsua = new frmSuaHang(gvChinh.CurrentRow.Cells[1].Value.ToString());
                if (frsua.ShowDialog() == DialogResult.Cancel)
                    XuatHangHoa();
            };
          
            btnXem.Name = "btnXem";
            btnXem.Text = "Xem chi tiết";
            btnXem.Click += (s, args) =>
            {
                frXemHang frxem = new frXemHang(gvChinh.CurrentRow.Cells[1].Value.ToString());
                frxem.ShowDialog();
            };
            btnXoa.Name = "btnXoa";
            btnXoa.Text = "Xóa";
            btnXoa.Click += (s, args) =>
            {
                string MaHang = gvChinh.CurrentRow.Cells[1].Value.ToString();
                DialogResult kq = MessageBox.Show("Bạn có muốn xóa: " + MaHang, "Thông báo", MessageBoxButtons.YesNo);
                if (kq == DialogResult.Yes)
                {
                    HangHoaBUS hh = new HangHoaBUS();
                    hh.XoaHangHoa(MaHang);
                    XuatHangHoa();
                }
            };

            this.Controls.Add(btnRefresh);
            this.Controls.Add(btnThem);
            this.Controls.Add(btnTim);
            this.Controls.Add(btnXem);
            this.Controls.Add(btnSua);
            this.Controls.Add(btnXoa);

            var stt = new DataGridViewTextBoxColumn();
            stt.HeaderText = "STT";
            stt.Name = "clStt";
            stt.Width = 50;

            var maHang = new DataGridViewTextBoxColumn();
            maHang.HeaderText = "Mã Hàng";
            maHang.Name = "clMaHang";
            maHang.Width = 80;

            var tenHang = new DataGridViewTextBoxColumn();
            tenHang.HeaderText = "Tên Hàng";
            tenHang.Name = "clTenHang";
            tenHang.Width = 400;

            var loaiHang = new DataGridViewTextBoxColumn();
            loaiHang.HeaderText = "Loại Hàng";
            loaiHang.Name = "clLoaiHang";
            loaiHang.Width = 200;

            var nhaCC = new DataGridViewTextBoxColumn();
            nhaCC.HeaderText = "Nhà Cung Cấp";
            nhaCC.Name = "clNhaCC";
            nhaCC.Width = 200;

            var soLuong = new DataGridViewTextBoxColumn();
            soLuong.HeaderText = "Số Lượng";
            soLuong.Name = "clSoLuong";
            soLuong.Width = 80;
            gvChinh.Columns.AddRange(new DataGridViewColumn[] { stt, maHang, tenHang, loaiHang, nhaCC, soLuong });


        }
        private void Refresh_GiaoDien()
        {
            this.Controls.Remove(btnRefresh);
            this.Controls.Remove(btnSua);
            this.Controls.Remove(btnThem);
            this.Controls.Remove(btnTim);
            this.Controls.Remove(btnXem);
            this.Controls.Remove(btnXoa);
            gvChinh.Columns.Clear();
        }
        private void XuatHangHoa()
        {
            HangHoaBUS hh = new HangHoaBUS();
            List<HangHoaDTO> listhh = hh.LayHangHoa();
            gvChinh.Rows.Clear();
            for (int i = 0; i < listhh.Count; i++)
            {
                LoaiHangDTO lh = new LoaiHangDTO();                
                LoaiHangBUS lhbus = new LoaiHangBUS();
                lh = lhbus.LayLoaiHangTheoMa(listhh[i].MaLoaiHang);

                NhaCCDTO ncc = new NhaCCDTO();
                NhaCCBUS nccbus = new NhaCCBUS();
                ncc = nccbus.LayNCCTheoMa(listhh[i].MaNCC);
                gvChinh.Rows.Add(i+1, listhh[i].MaHang, listhh[i].TenHang, lh.TenLoaiHang, ncc.TenNCC, listhh[i].SoLuong);
            }
        }

        private void loạiHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frCDLoaiHang cdlh = new frCDLoaiHang();
            cdlh.ShowDialog();
        }

        private void nhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frCDNhaCC frNhaCC = new frCDNhaCC();
            frNhaCC.ShowDialog();
        }

        private void hàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Refresh_GiaoDien();
            HangHoa_GUI();
            XuatHangHoa();
        }        
    }
}
